package cn.git.service.impl;

import cn.git.entity.Person;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.camunda.bpm.model.bpmn.instance.Task;
import cn.git.service.CacheService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 缓存服务实现类
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-09-23
 */
@Service
public class CacheServiceImpl implements CacheService {

    /**
     * 根据id获取person
     *
     * @param id id
     */
    @Cacheable(cacheNames = "person", key = "#id")
    @Override
    public Person getPersonById(String id) {
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Person person = new Person();
        person.setId(id);
        person.setAge(18);
        person.setSex("男");
        person.setName("张三");
        System.out.println("获取数据成功 -> " + JSONObject.toJSONString(person));
        return person;
    }

    /**
     * 根据id获取person,缓存key自定义
     *
     * @param id id
     */
    @Cacheable(cacheNames = "person", keyGenerator = "customKeyGenerator")
    @Override
    public Person getPersonByCusId(String id) {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Person person = new Person();
        person.setId(id);
        person.setAge(18);
        person.setSex("男");
        person.setName("张三");
        System.out.println("获取数据成功 -> " + JSONObject.toJSONString(person));
        return person;
    }

    /**
     * 更新person, 并返回person
     * 返回person值会被缓存，缓存的key为person的id
     *
     * @param person person
     */
    @CachePut(cacheNames = "person", key = "#person.id")
    @Override
    public Person updatePerson(Person person) {
        person.setName("李四".concat(String.valueOf(System.currentTimeMillis())));
        System.out.println("更新数据成功 -> " + JSONObject.toJSONString(person));
        return person;
    }

    /**
     * 根据id删除person
     * 使用参数 allEntries=true 则可以删除所有缓存
     *
     * @param id id
     */
    @CacheEvict(cacheNames = "person", key = "#id")
    @Override
    public void deletePersonById(String id) {
        System.out.println("数据删除成功 -> " + id);
    }

    /**
     * 获取bpmn信息
     *
     * @param file file
     */
    @Override
    public void getBpmnInfoByFile(MultipartFile file) {
        // 解析文件内容,分别获取Task任务列表以及全部节点信息
        List<String> fileChainList = new ArrayList<>();
        try (InputStream inputStream = file.getInputStream();) {
            // 解析bpmn xml文件信息
            BpmnModelInstance modelInstance = Bpmn.readModelFromStream(inputStream);
            // 类型比较
            List<FlowElement> flowElements = (List<FlowElement>) modelInstance.getModelElementsByType(FlowElement.class);
            for (FlowElement element : flowElements) {
                Class<? extends FlowElement> elementType = element.getClass();
                // 获取类型名称
                if (elementType.getSimpleName().equals("ExclusiveGatewayImpl")) {
                    System.out.println(elementType.getSimpleName());
                }
            }

            // task任务节点数量比较
            for (Task task : modelInstance.getModelElementsByType(Task.class)) {
                String taskName = task.getName();
                fileChainList.add(taskName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
